package com.example.mycityapp;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.mycityapp.BrowsePictureActivity.BrowsePictureForWhat;
import com.example.mycityapp.util.ImageDownloader;
import com.example.mycityapp.util.StringReplacer;

import android.R.string;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class NewPostPage extends Activity {
	public static NewPostPage instance;
	public static String postContent = "";
	public static String tags = "";
	public static String postType = "";
	public static int locationId = 1;
	public static String locationName = "";
	public static String imgurId="";//x4KSWG3 (code for an example imgurId)
	
	public static TextView locationText; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.newpostpage);
		instance = NewPostPage.this;
		TextView userNameTextView = (TextView) findViewById(R.id.usernametextView);
		userNameTextView.setText(MainActivity.firstName + " " + MainActivity.lastName);
		EditText contentEditText = (EditText) findViewById(R.id.newpostcontent);
		contentEditText.setText(postContent);
		EditText tagsEditText = (EditText) findViewById(R.id.newpostlocation);
		tagsEditText.setText(tags);
		locationText = (TextView) findViewById(R.id.TextViewShowLocation);
		if (!imgurId.isEmpty())
		{
			Log.d("", imgurId);
			ImageView imageViewNewPost = (ImageView) findViewById(R.id.imageViewNewPost);
			ImageDownloader.download("http://i.imgur.com/"+imgurId+".jpg", imageViewNewPost, true);
			Log.d("", imgurId);
		}
		
		Button addMedia = (Button) findViewById(R.id.newPostAddMultimediaButton);
		addMedia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText contentEditText = (EditText) findViewById(R.id.newpostcontent);
				postContent = StringReplacer.Encode(contentEditText.getText().toString());
				EditText tagsEditText = (EditText) findViewById(R.id.newpostlocation);
				tags = StringReplacer.Encode(tagsEditText.getText().toString());
				BrowsePictureActivity.browseType = BrowsePictureForWhat.NewPostPage;
				startActivity(new Intent(NewPostPage.this,
						BrowsePictureActivity.class));
			}
		});

		Button addLocation = (Button) findViewById(R.id.newpostAddLocation);
		addLocation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(NewPostPage.this,
						MapActivity.class));
			}
		});
		
		Button addPost = (Button) findViewById(R.id.newpostpostbutton);
		addPost.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText contentEditText = (EditText) findViewById(R.id.newpostcontent);
				postContent = StringReplacer.Encode(contentEditText.getText().toString());
				EditText tagsEditText = (EditText) findViewById(R.id.newpostlocation);
				tags = StringReplacer.Encode(tagsEditText.getText().toString());
				if (postContent.isEmpty()) {
					AlertDialog.Builder dlgAlert = new AlertDialog.Builder(NewPostPage.this);

					dlgAlert.setMessage("Your post contains no text!");
					dlgAlert.setTitle("Error Message...");
					dlgAlert.setPositiveButton("OK", null);
					dlgAlert.setCancelable(true);
					dlgAlert.create().show();

					dlgAlert.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {

								}
							});

				}
				else{
					if(imgurId.isEmpty()){
						postType = "1";
					}
					else{
						postType = "2";
					}
					String content = "";
					if (postType == "2")
					{
						content = StringReplacer.Encode(postContent + "|" + imgurId);
					}
					else
					{
						content = postContent;
					}
					
					String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/post?userId=" + MainActivity.userId +
							"&postType=" + postType + "&postContent=" + content + "&tags=" + tags + "&locationId=" + locationId + "&locationName=" + locationName;
					
					String urlResponse = JSONParser.getJSONFromUrl(url);
					
					if (urlResponse.contains("-1"))
					{
						AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(NewPostPage.this);
						
	                    dlgAlert.setMessage("Please enter a location.");
	                    dlgAlert.setTitle("Error Message...");
	                    dlgAlert.setPositiveButton("OK", null);
	                    dlgAlert.setCancelable(true);
	                    dlgAlert.create().show();

	                    dlgAlert.setPositiveButton("Ok",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int which) {

	                        }
	                    });
					}
					else
					{
						NewPostPage.this.finish();
					}
				}
			}
		});
	}
}
